﻿// ———————————————————————–
// <copyright file="EventComment.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// EventComment.cs:
//
// A note associated with a CAD message
//
// Project: IC.NET - NIEM Core Library
// Author:  Jim Cronin - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Xml;

namespace EDXLSharp.NIEMCore
{
  /// <summary>
  /// A note about a CAD item
  /// </summary>
  public class EventComment : ComplexTypeBase
  {
    public override string ElementName
    {
      get { return "EventComment"; }
    }
    public override string ElementNamespace
    {
      get { return EDXLConstants.EMEventNamespace; }
    }
    public override string ElementPrefix
    {
      get { return EDXLConstants.EMEventPrefix; }
    }

    /// <summary>
    /// Text of the note
    /// </summary>
    public string Text { get; set; }

    /// <summary>
    /// DateTime this note was made
    /// </summary>
    public DateTime DateTime { get; set; }

    /// <summary>
    /// Contact who made the note
    /// </summary>
    public ContactInformation ContactInformation { get; set; }

    /// <summary>
    /// Initializes a new instance of the Comment class
    /// </summary>
    public EventComment()
    {
    }

    /// <summary>
    /// Initializes a new instance of the Comment class from an XML string
    /// </summary>
    /// <param name="xmlString">Valid XML Comment data in a string</param>
    /// <exception cref="System.ArgumentException">Thrown if xmlString is null, empty, whitespace, or invalid XML</exception>
    public EventComment(string xmlString)
      : this()
    {
      if (string.IsNullOrWhiteSpace(xmlString))
      {
        throw new ArgumentNullException("xmlString", "XML must not be null or blank");
      }

      XmlDocument doc = new XmlDocument();
      try
      {
        doc.Load(xmlString);
      }
      catch (XmlException xe)
      {
        throw new ArgumentException("Error parsing XML. See inner exception for details", xe);
      }

      this.ReadXML(doc.DocumentElement);
    }

    /// <summary>
    /// Writes this Object to an inline XML Document
    /// </summary>
    /// <param name="xwriter">Valid XMLWriter</param>
    public override void WriteXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      this.Validate();

      xwriter.WriteStartElement(EDXLConstants.CADPrefix, this.ElementName, EDXLConstants.CADNamespace);
      xwriter.WriteElementString(EDXLConstants.NiemCorePrefix, "DateTime", EDXLConstants.NiemCoreNamespace, this.DateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.ffZ"));
      this.ContactInformation.WriteXML(xwriter);
      xwriter.WriteElementString(EDXLConstants.NiemCorePrefix, "CommentText", EDXLConstants.NiemCoreNamespace, this.Text);
      xwriter.WriteEndElement();
    }

    /// <summary>
    /// Reads this Object's values from an XML NodeList
    /// </summary>
    /// <param name="rootnode">Root XML Node of the Object Element</param>
    public override void ReadXML(XmlNode rootnode)
    {
      if (rootnode == null)
      {
        throw new ArgumentNullException("rootnode");
      }

      if (rootnode.LocalName != this.ElementName)
      {
        throw new ArgumentException("The node is not a(n) " + this.ElementName);
      }

      foreach (XmlNode node in rootnode.ChildNodes)
      {
        switch (node.LocalName)
        {
          case "DateTime":
            this.DateTime = DateTime.Parse(node.InnerText);
            break;
          case "ContactInformation":
            this.ContactInformation = new ContactInformation();
            break;
          case "CommentText":
            this.Text = node.InnerText;
            break;
          default:
            throw new ArgumentException("Unkown child element: " + node.LocalName);
        }
      }

      this.Validate();
    }

    /// <summary>
    /// Validates the note object
    /// </summary>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if validation fails</exception>
    public override void Validate()
    {
      if (this.ContactInformation == null)
      {
        throw new ValidationException("Comment Contact Information is missing");
      }
      //this.ContactInformation.Validate();
    }
  }
}
